<?php

class Zend_Controller_Action_Helper_Email extends Zend_Controller_Action_Helper_Abstract 
{
    private function sendEmail($to,$subject,$message)
    {
    	$configInfo = array('auth' => 'login',
							'ssl' => 'tls',
							'username' => 'weimingge@gmail.com',
							'password' => 'iloveme1226',
							'port' => 587);
	
		$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $configInfo);
		Zend_Mail::setDefaultTransport($smtpHost);
		
		$MailObj = new Zend_Mail('utf-8');
		
    	$fromEmail = "no-reply@weimingge.com";
        $fromFullName = "邀请卡";
        
        $MailObj->setBodyText(strip_tags($message));
        $MailObj->setBodyHtml($message);
        $MailObj->setFrom($fromEmail,$fromFullName);
        $MailObj->addTo($to);
        $MailObj->setSubject($subject);
        try{
        	$MailObj->send();
        	return "Email sent successfully!";
        } catch (Zend_Mail_Exception $e) {
        	return $e->getMessage();
        }
        		
    }

	public function direct($to, $key)
    {
    	$config = Zend_Registry::get('config');
    	$message = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><a href='".$config->baseurl."/user/activate?key=";
    	$message .= $key."&email=".$to."'>activate!</a>";
    	$this->sendEmail($to,'激活账户',$message);
    }
    
    /**
     * Send activity email to user.
     * 
     * @param $activityType
     * @param $to
     * @param $subject
     * @param $data
     */
    public function sendActivityEmail($activityType, $to, $subject, $data) {
    	$message = EmailMessageTemplate::getMessage($activityType, $data);
    	$this->sendEmail($to, $subject, $message);
    }
    
    public function addToMailQueue($activityType, $subject, $recipients, $message) {
    	
    }
    
    /**
     * Send invitation.
     */
    public function sendInvitation($to, $subject, $message) {
    	$result=$this->sendEmail($to, $subject, $message);
    	if($result == 'Email sent successfully!') {
    		return 0;
    	} else {
    		return 1;
    	}
    }
	
}